package cn.tedu.ivos.application.service.impl;

import cn.tedu.ivos.application.mapper.ApplicationMapper;
import cn.tedu.ivos.application.pojo.dio.ApplicationQuery;
import cn.tedu.ivos.application.pojo.dio.ApplicationSaveParam;
import cn.tedu.ivos.application.pojo.entity.Application;
import cn.tedu.ivos.application.pojo.vo.ApplicationVO;
import cn.tedu.ivos.application.service.ApplicationService;
import cn.tedu.ivos.audit.mapper.AuditMapper;
import cn.tedu.ivos.audit.pojo.vo.AuditVO;
import cn.tedu.ivos.audit.service.AuditService;
import cn.tedu.ivos.base.enums.ApplicationStatusEnum;
import cn.tedu.ivos.user.mapper.UserMapper;
import cn.tedu.ivos.user.pojo.vo.UserVO;
import cn.tedu.ivos.vehicle.mapper.VehicleMapper;
import cn.tedu.ivos.vehicle.pojo.entity.Vehicle;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.StringJoiner;

@Transactional
@Slf4j
@Service
public class ApplicationServiceImpl implements ApplicationService {
	@Autowired
	ApplicationMapper applicationMapper;
	
	@Autowired
	AuditService auditService;
	@Autowired
	UserMapper userMapper;
	@Autowired
	AuditMapper auditMapper;
	@Autowired
	VehicleMapper vehicleMapper;
	
	@Override
	public void saveApplication(ApplicationSaveParam applicationSaveParam) {
		log.debug("新增申请单业务,参数={}",applicationSaveParam);
		Application application = new Application();
		BeanUtils.copyProperties(applicationSaveParam,application);
		//设置当前申请单的状态为"已发起",使用枚举!
		application.setStatus(ApplicationStatusEnum.PENDING.getCode());
		application.setCreateTime(new Date());
		applicationMapper.insert(application);
		
		//还需要生成此申请对应的审批单数据
		auditService.insertAudit(application);
	}
	
	@Override
	public List<ApplicationVO> selectApplication(ApplicationQuery applicationQuery) {
		log.debug("查询申请单业务,参数={}",applicationQuery);
		List<ApplicationVO> list =
				applicationMapper.selectApplication(applicationQuery);
		for (int i = 0; i< list.size(); i++){
			ApplicationVO applicationVO = list.get(i);
			assignAuditUserList(applicationVO);
		}
		return list;
	}
	
	@Override
	public void cancel(Long id) {
		log.debug("撤销申请单业务,参数={}",id);
		Application application = new Application();
		application.setId(id);
		application.setStatus(ApplicationStatusEnum.CANCEL.getCode());
		application.setUpdateTime(new Date());
		applicationMapper.update(application);
		
		//还需要删除此申请单对应的所有审批单
		auditMapper.deleteByApplicationId(id);
	}
	
	@Override
	public void distribute(Long applicationId, Long vehicleId) {
		log.debug("分配用车业务,参数={},{}",applicationId,vehicleId);
		Application application = new Application();
		application.setId(applicationId);
		application.setVehicleId(vehicleId);
		application.setStatus(ApplicationStatusEnum.ALLOCATION.getCode());
		application.setUpdateTime(new Date());
		applicationMapper.update(application);
		
		Vehicle vehicle = new Vehicle();
		vehicle.setId(vehicleId);
		vehicle.setStatus("2");
		vehicle.setUpdateTime(new Date());
		vehicleMapper.update(vehicle);
	}
	
	@Override
	public void back(Long applicationId, Long vehicleId) {
		log.debug("还车业务,参数={},{}",applicationId,vehicleId);
		Application application = new Application();
		application.setId(applicationId);
		application.setVehicleId(null);
		application.setStatus(ApplicationStatusEnum.END.getCode());
		application.setUpdateTime(new Date());
		applicationMapper.back(application);
		
		Vehicle vehicle = new Vehicle();
		vehicle.setId(vehicleId);
		vehicle.setStatus("1");
		vehicle.setUpdateTime(new Date());
		vehicleMapper.update(vehicle);
		
	}
	
	
	private void assignAuditUserList(ApplicationVO applicationVO) {
		List<Long> auditUserIdList = new ArrayList<>();
		List<String> auditUsernameList = new ArrayList<>();
		List<AuditVO> auditVOList =
				auditService.selectAuditByApplicationId(applicationVO.getId());
		for(int i = 0; i < auditVOList.size(); i++){
			AuditVO auditVO = auditVOList.get(i);
			Long id = auditVO.getAuditUserId();
			auditUserIdList.add(id);
			UserVO userVO = userMapper.selectById(id);
			auditUsernameList.add(userVO.getUsername());
		}
		StringJoiner stringJoiner = new StringJoiner(",");
		for(String username : auditUsernameList){
			stringJoiner.add(username);
		}
		applicationVO.setAuditUsernameList(stringJoiner.toString());
		applicationVO.setAuditUserIdList(auditUserIdList);
	}
}

